package com.heisen.uua.translator;

import com.heisen.base.constant.HttpConstant;
import com.heisen.base.entity.Api;
import com.heisen.base.exception.RRException;
import enums.ApiReturnEnums;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class XmWebResponseExceptionTranslator implements WebResponseExceptionTranslator {

    @Override
    public ResponseEntity translate(Exception e) {
        ResponseEntity.BodyBuilder status = ResponseEntity.status(HttpServletResponse.SC_OK);

        log.error("【认证异常】={}", e.getMessage());
        if (e instanceof UnsupportedGrantTypeException) {
            String grantType = StringUtils.substringAfterLast(e.getMessage(), ": ");
            return status.body(Api.failFormat(ApiReturnEnums.AUTHENTICATION_TYPE_ISNOT, grantType));
        }

        if (e instanceof RRException) {
            return status.body(Api.fail(e.getMessage()));
        }

        if (e instanceof InternalAuthenticationServiceException) {
            if (StringUtils.containsIgnoreCase(e.getMessage(), "You have an error in your SQL syntax")) {
                return status.body(Api.fail(ApiReturnEnums.SQL_ERRER));
            }
            return status.body(Api.fail(e.getMessage()));
        }

        if (e instanceof InvalidRequestException) {
            if (StringUtils.containsIgnoreCase(e.getMessage(), "Missing grant type")) {
                return status.body(Api.fail(ApiReturnEnums.NOT_FIND_GRANT_TYPE));
            }
        }

        if (e instanceof InvalidGrantException) {
            if (StringUtils.containsIgnoreCase(e.getMessage(), "Invalid refresh token")) {
                status = ResponseEntity.status(HttpStatus.UNAUTHORIZED);
                return status.body(Api.fail(ApiReturnEnums.REFRESH_TOKEN_INVALID));
            }
            if (StringUtils.containsIgnoreCase(e.getMessage(), "locked")) {
                return status.body(Api.fail(ApiReturnEnums.USER_ISLOCK));
            }
            if (StringUtils.containsIgnoreCase(e.getMessage(), "Bad credentials")) {
                return status.body(Api.fail(ApiReturnEnums.PASSWORD_ERRER));
            }

        }

        if (e instanceof InvalidClientException) {
            if (StringUtils.containsIgnoreCase(e.getMessage(), "Unauthorized grant type")) {
                String grantType = StringUtils.substringAfterLast(e.getMessage(), ": ");
                return status.body(Api.failFormat(ApiReturnEnums.AUTHENTICATION_TYPE_ISNOT, grantType));
            }
        }

        if (e instanceof BadCredentialsException) {
            if (StringUtils.containsIgnoreCase(e.getMessage(), "Unauthorized grant type")) {
                return status.body(Api.fail(ApiReturnEnums.PASSWORD_ERRER));
            }

            if (StringUtils.containsIgnoreCase(e.getMessage(), "Bad credentials")) {
                return status.body(Api.fail(ApiReturnEnums.PASSWORD_ERRER));
            }
        }

        if (e instanceof IncorrectResultSizeDataAccessException) {
            return status.body(Api.fail(ApiReturnEnums.OAUTH_TOKEN_CONCURRENCY));
        }

        return status.body(Api.fail(ApiReturnEnums.AUTHENTICATION_FAIL));

    }
}
